<template>
  <div class="app-container">
    <el-table :data="tableData" style="width: 100%" border>
      <el-table-column prop="userId" label="用户id" width="180" />
      <el-table-column prop="userName" label="用户名" width="180" />
      <el-table-column prop="name" label="用户名称" />
      <el-table-column prop="introduction" label="简介" />
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="handleClick(scope.row)">分配用户角色</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="分配用户角色" :visible.sync="roleChangeVisible" width="30%">
      <el-radio-group v-model="params.checkRole">
        <el-radio v-for="item in roles" :key="item.roleId" :label="item.roleId">{{ item.roleName }}</el-radio>
      </el-radio-group>
      <div>
        <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="changeRole">确定</el-button>
          <el-button @click="roleChangeVisible = false">取 消</el-button>
        </span>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getAllAdmminInfo, getRole, chageRole } from '@/api/admin'

export default {
  name: 'Test',
  data() {
    return {
      // 表格数据
      tableData: [],
      // 控制选择角色框是否出现
      roleChangeVisible: false,

      // 角色列表
      roles: [],

      params: {
        // 用户选中的角色
        checkRole: '',
        // 之前角色选择的id
        beforecheckRole: '',
        userId: ''
      }
    }
  },
  created() {
    this.init()
  },
  methods: {
    // 初始化
    init() {
      getAllAdmminInfo().then((res) => {
        this.tableData = res.data
      })
      getRole().then((res) => {
        this.roles = res.data
      })
    },

    handleClick(row) {
      // 打开显示框
      this.roleChangeVisible = true
      //
      console.log(row)
      // 命中选择的角色
      this.params.checkRole = row.roleId
      this.params.beforecheckRole = row.roleId
      this.params.userId = row.userId
    },

    // 分配角色
    changeRole() {
      chageRole(this.params).then((res) => {
        if (res.code == 200) {
          this.roleChangeVisible = false
          this.init()
        }
      })
    }
  }
}
</script>
